package whf.framework.exception;


import whf.framework.i18n.ApplicationResource;
import whf.framework.meta.Meta;
import whf.framework.resource.Resource;

/**
 * 所有应用异常的超类
 * @author wanghaifeng
 *
 */
@SuppressWarnings("serial")
public class AppException extends Exception{
	private static final long serialVersionUID=0;
	
	/**
	 * 异常发生对象
	 */
	private Meta meta;
	
	/**
	 * 异常发生源
	 */
	private Object source;
	
	/**
	 * 异常导致原因
	 */
	private Throwable cause;
	/**
	 * 异常严重程度
	 */
	private ExceptionSeverity severity;
	
	/**
	 * 异常消息定义，可以由前述属性决定
	 */
	private Resource resource;
	
	public AppException() {
		super();
	}

	public AppException(String message, Throwable cause) {
		super(message, cause);
		this.cause = cause;
	}

	public AppException(String message) {
		super(message);
	}

	public AppException(Throwable cause) {
		super(cause);
		this.cause = cause;
	}
	
	public String getErrorTitle(){
		return "系统发生应用级别错误!";
	}

	public Throwable getCause() {
		return cause;
	}

	public void setCause(Throwable cause) {
		this.cause = cause;
	}

	public Meta getMeta() {
		return meta;
	}

	public void setMeta(Meta meta) {
		this.meta = meta;
	}

	public Resource getResource() {
		return resource;
	}

	public void setResource(Resource resource) {
		this.resource = resource;
	}

	public ExceptionSeverity getSeverity() {
		return severity;
	}

	public void setSeverity(ExceptionSeverity severity) {
		this.severity = severity;
	}

	public Object getSource() {
		return source;
	}

	public void setSource(Object source) {
		this.source = source;
	}
	
	public String getMessage(){
		String msg = super.getMessage();
		if(msg != null && msg.startsWith("$i18n.")){
			msg = ApplicationResource.get(msg.substring(6));
		}
		return msg;
	}
}
